{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3.5 \u5b57\u8282\u5230\u5927\u6574\u6570\u7684\u6253\u5305\u4e0e\u89e3\u5305\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u5e76\u60f3\u5c06\u5b83\u89e3\u538b\u6210\u4e00\u4e2a\u6574\u6570\u3002\u6216\u8005\uff0c\u4f60\u9700\u8981\u5c06\u4e00\u4e2a\u5927\u6574\u6570\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5047\u8bbe\u4f60\u7684\u7a0b\u5e8f\u9700\u8981\u5904\u7406\u4e00\u4e2a\u62e5\u6709128\u4f4d\u957f\u768416\u4e2a\u5143\u7d20\u7684\u5b57\u8282\u5b57\u7b26\u4e32\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "data = b'\\x00\\x124V\\x00x\\x90\\xab\\x00\\xcd\\xef\\x01\\x00#\\x004'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5c06bytes\u89e3\u6790\u4e3a\u6574\u6570\uff0c\u4f7f\u7528 int.from_bytes() \u65b9\u6cd5\uff0c\u5e76\u50cf\u4e0b\u9762\u8fd9\u6837\u6307\u5b9a\u5b57\u8282\u987a\u5e8f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "len(data)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "int.from_bytes(data, 'little')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "int.from_bytes(data, 'big')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5c06\u4e00\u4e2a\u5927\u6574\u6570\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 int.to_bytes() \u65b9\u6cd5\uff0c\u5e76\u50cf\u4e0b\u9762\u8fd9\u6837\u6307\u5b9a\u5b57\u8282\u6570\u548c\u5b57\u8282\u987a\u5e8f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x = 94522842520747284487117727783387188\nx.to_bytes(16, 'big')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x.to_bytes(16, 'little')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5927\u6574\u6570\u548c\u5b57\u8282\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u8f6c\u6362\u64cd\u4f5c\u5e76\u4e0d\u5e38\u89c1\u3002\n\u7136\u800c\uff0c\u5728\u4e00\u4e9b\u5e94\u7528\u9886\u57df\u6709\u65f6\u5019\u4e5f\u4f1a\u51fa\u73b0\uff0c\u6bd4\u5982\u5bc6\u7801\u5b66\u6216\u8005\u7f51\u7edc\u3002\n\u4f8b\u5982\uff0cIPv6\u7f51\u7edc\u5730\u5740\u4f7f\u7528\u4e00\u4e2a128\u4f4d\u7684\u6574\u6570\u8868\u793a\u3002\n\u5982\u679c\u4f60\u8981\u4ece\u4e00\u4e2a\u6570\u636e\u8bb0\u5f55\u4e2d\u63d0\u53d6\u8fd9\u6837\u7684\u503c\u7684\u65f6\u5019\uff0c\u4f60\u5c31\u4f1a\u9762\u5bf9\u8fd9\u6837\u7684\u95ee\u9898\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u4e00\u79cd\u66ff\u4ee3\u65b9\u6848\uff0c\u4f60\u53ef\u80fd\u60f3\u4f7f\u75286.11\u5c0f\u8282\u4e2d\u6240\u4ecb\u7ecd\u7684 struct \u6a21\u5757\u6765\u89e3\u538b\u5b57\u8282\u3002\n\u8fd9\u6837\u4e5f\u884c\u5f97\u901a\uff0c\u4e0d\u8fc7\u5229\u7528 struct \u6a21\u5757\u6765\u89e3\u538b\u5bf9\u4e8e\u6574\u6570\u7684\u5927\u5c0f\u662f\u6709\u9650\u5236\u7684\u3002\n\u56e0\u6b64\uff0c\u4f60\u53ef\u80fd\u60f3\u89e3\u538b\u591a\u4e2a\u5b57\u8282\u4e32\u5e76\u5c06\u7ed3\u679c\u5408\u5e76\u4e3a\u6700\u7ec8\u7684\u7ed3\u679c\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import struct\nhi, lo = struct.unpack('>QQ', data)\n(hi << 64) + lo"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b57\u8282\u987a\u5e8f\u89c4\u5219(little\u6216big)\u4ec5\u4ec5\u6307\u5b9a\u4e86\u6784\u5efa\u6574\u6570\u65f6\u7684\u5b57\u8282\u7684\u4f4e\u4f4d\u9ad8\u4f4d\u6392\u5217\u65b9\u5f0f\u3002\n\u6211\u4eec\u4ece\u4e0b\u9762\u7cbe\u5fc3\u6784\u9020\u768416\u8fdb\u5236\u6570\u7684\u8868\u793a\u4e2d\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u770b\u51fa\u6765\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x = 0x01020304\nx.to_bytes(4, 'big')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x.to_bytes(4, 'little')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8bd5\u7740\u5c06\u4e00\u4e2a\u6574\u6570\u6253\u5305\u4e3a\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u90a3\u4e48\u5b83\u5c31\u4e0d\u5408\u9002\u4e86\uff0c\u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\u9519\u8bef\u3002\n\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 int.bit_length() \u65b9\u6cd5\u6765\u51b3\u5b9a\u9700\u8981\u591a\u5c11\u5b57\u8282\u4f4d\u6765\u5b58\u50a8\u8fd9\u4e2a\u503c\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x = 523 ** 23\nx"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x.to_bytes(16, 'little')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x.bit_length()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "nbytes, rem = divmod(x.bit_length(), 8)\nif rem:\nnbytes += 1\nx.to_bytes(nbytes, 'little')"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}